import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/component/InheritedWidgetContext.dart';

class InheritedWidget_Test extends StatefulWidget {
  const InheritedWidget_Test({Key? key}) : super(key: key);

  @override
  _InheritedWidget_TestState createState() => _InheritedWidget_TestState();
}

class _InheritedWidget_TestState extends State<InheritedWidget_Test> {
  late InheritedTestModel _inheritedTestModel;

  @override
  void initState() {
    _initData();
    // TODO: implement initState
    super.initState();
  }

  _initData() {
    _inheritedTestModel = InheritedTestModel(0);
  }

  _incrementCount() {
    setState(() {
      _inheritedTestModel = InheritedTestModel(_inheritedTestModel!.count + 1);
    });
  }

  _reduceCount() {
    setState(() {
      _inheritedTestModel = InheritedTestModel(_inheritedTestModel!.count - 1);
    });
  }

  @override
  Widget build(BuildContext context) {
    return  InheritedWidgetContext(
        inheritedTestModel: _inheritedTestModel,
        incerment: _incrementCount,
        reduce: _reduceCount,
        child: Scaffold(
            appBar: AppBar(
              title: Text("InherItedWidget"),
            ),
            body: Center(
              child: Column(
                children: <Widget>[
                  Text("${_inheritedTestModel.count}"),
                  TestWidgetA(),
                  TestWidgetC(),
                  TestWidgetB()
                ],
              ),
            )
        ),
    );
  }
}

class TestWidgetA extends StatelessWidget {
  const TestWidgetA({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final inheritedContext = InheritedWidgetContext.of(context);
    incerment() {
      inheritedContext!.incerment();
    }

    return RaisedButton(
      onPressed: incerment,
      child: Text("开始加+"),
    );
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    print("2222");
  }
}

class TestWidgetB extends StatelessWidget {
  const TestWidgetB({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final inheritedContext = InheritedWidgetContext.of(context);
    reduce() {
      inheritedContext!.reduce();
    }

    return RaisedButton(
      onPressed: reduce,
      child: Text("开始减-"),
    );
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    print("333");
  }
}

// class TestWidgetC extends StatelessWidget {
//   const TestWidgetC({Key? key}) : super(key: key);
//
//   @override
//   Widget build(BuildContext context) {
//     final inheritedContext = InheritedWidgetContext.of(context);
//     final inheritedTestModel = inheritedContext!.inheritedTestModel;
//     return Container(
//       child: Text("当前数字${inheritedTestModel.count}"),
//     );
//   }
//
//   @override
//   void didChangeDependencies() {
//     // build方法中没有依赖InheritedWidget，此回调不会被调用。
//     print("Dependencies change");
//   }
// }
class TestWidgetC extends StatefulWidget {
  const TestWidgetC({Key? key}) : super(key: key);

  @override
  _TestWidgetCState createState() => _TestWidgetCState();
}

class _TestWidgetCState extends State<TestWidgetC> {
  @override
  Widget build(BuildContext context) {
    final inheritedContext = InheritedWidgetContext.of(context);
    final inheritedTestModel = inheritedContext!.inheritedTestModel;
    return Container(
      child: Text("当前数字${inheritedTestModel.count}"),
    );
  }
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    //父或祖先widget中的InheritedWidget改变(updateShouldNotify返回true)时会被调用。
    //如果build中没有依赖InheritedWidget，则此回调不会被调用。
    print("=======");
  }
}

